/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using SlimDX;
using SlimDX.Direct3D11;
using JBBRXG11;

namespace JBBRXG11
{
    public class IndexBuffer : IDisposable // GraphicsResource
    {
        GraphicsDevice device;
        SlimDX.Direct3D11.Buffer dxbuffer;
        int size;
        BufferUsage bufferusage;
        Microsoft.Xna.Framework.Graphics.IndexElementSize elsize;

        internal SlimDX.Direct3D11.Buffer DX_Buffer { get { return dxbuffer; } }
        public BufferUsage BufferUsage { get { return bufferusage; } }
        public Microsoft.Xna.Framework.Graphics.IndexElementSize IndexElementSize { get { return elsize; } }
        public int SizeInBytes { get { return size; } }

        public IndexBuffer(GraphicsDevice graphicsDevice,
                           int sizeInBytes,
                           BufferUsage usage,
                           Microsoft.Xna.Framework.Graphics.IndexElementSize elementSize)
        {
            device = graphicsDevice;
            size = sizeInBytes;
            bufferusage = usage;
            elsize = elementSize;

            BufferDescription bd;
            bd = new BufferDescription();
            bd.Usage = ResourceUsage.Dynamic;
            bd.SizeInBytes = size;
            bd.BindFlags = BindFlags.IndexBuffer;
            if ((usage & BufferUsage.Points) != 0)
            {
                throw new Exception("JBBRXG11 NYI: Can't do anything with point sprites");
            }
            if ((usage & BufferUsage.StreamOut) != 0)
            {
                throw new Exception("JBBRXG11 NYI?: Index buffers cannot have stream out usage");
            }
            if ((usage & BufferUsage.WriteOnly) == 0)
            {
                bd.CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write;
            }
            else
            {
                bd.CpuAccessFlags = CpuAccessFlags.Write;
            }
            bd.OptionFlags = ResourceOptionFlags.None;
            dxbuffer = new SlimDX.Direct3D11.Buffer(device.DX_Device, bd);
            GraphicsDeviceManager.GetGame(device).UserContentAdd(this);
        }

        public IndexBuffer(GraphicsDevice graphicsDevice, Type indexType, int elementCount,
                           BufferUsage usage)
        {
            throw new Exception("JBBRXG11 NYI: can't cope with this index buffer constructor");
        }

        public void SetData<T>(T[] data) where T : struct
        {
            DataBox box = device.DX_Device_Context.MapSubresource(dxbuffer, MapMode.WriteDiscard, MapFlags.None);
            box.Data.WriteRange<T>(data);
            device.DX_Device_Context.UnmapSubresource(dxbuffer, 0);
            //DataStream stream = new DataStream((data.Length * 2)-1, true, true);//dxbuffer.Map(MapMode.WriteDiscard, MapFlags.None);
            //stream.Position = 0;
            //stream.WriteRange<T>(data);
            
            //SlimDX.Direct3D11.Buffer indexBuffer = new SlimDX.Direct3D11.Buffer(device.DX_Device, stream, (int)stream.Length, ResourceUsage.Dynamic, BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);

            //DataBox box = device.DX_Device_Context.MapSubresource(indexBuffer, MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None);
            //box.Data.Position = 0;
            //box.Data.WriteRange<T>(data);
            //device.DX_Device_Context.UnmapSubresource(indexBuffer, 0);
        }

        public void Dispose()
        {
            dxbuffer.Dispose();
        }
    }
}

/*
        // Summary:
        //     Immediately releases the unmanaged resources used by this object.
        //
        // Parameters:
        //   :
        //     [MarshalAsAttribute(U1)] true to release both managed and unmanaged resources;
        //     false to release only unmanaged resources.
        protected override void Dispose(bool __p1);
        //
        // Summary:
        //     Copies the index buffer into an array.
        //
        // Parameters:
        //   data:
        //     The array to receive index buffer data.
        public void GetData<T>(T[] data);
        //
        // Summary:
        //     Copies the index buffer into an array.
        //
        // Parameters:
        //   data:
        //     The array to receive index buffer data.
        //
        //   startIndex:
        //     The index of the element in the array at which to start copying.
        //
        //   elementCount:
        //     The number of elements to copy.
        public void GetData<T>(T[] data, int startIndex, int elementCount);
        //
        // Summary:
        //     Copies the index buffer into an array.
        //
        // Parameters:
        //   offsetInBytes:
        //     The number of bytes into the index buffer where copying will start.
        //
        //   data:
        //     The array to receive index buffer data.
        //
        //   startIndex:
        //     The index of the element in the array at which to start copying.
        //
        //   elementCount:
        //     The number of elements to copy.
        public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount);

        //
        // Summary:
        //     Copies array data to the index buffer.
        //
        // Parameters:
        //   data:
        //     The array of data to copy.
        //
        //   startIndex:
        //     The index of the element in the array at which to start copying.
        //
        //   elementCount:
        //     The number of elements to copy.
        public void SetData<T>(T[] data, int startIndex, int elementCount);
        //
        // Summary:
        //     Copies array data to the index buffer.
        //
        // Parameters:
        //   offsetInBytes:
        //     Number of bytes into the index buffer where copying will start.
        //
        //   data:
        //     The array of data to copy.
        //
        //   startIndex:
        //     The index of the element in the array at which to start copying.
        //
        //   elementCount:
        //     The number of elements to copy.
        public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount);
        //
        // Summary:
        //     Retrieves a string representation of this object.
        public override string ToString();*/
